package jwj8508.j2ee.basic.projectv10.dao;

import java.util.List;

import jwj8508.j2ee.basic.projectv10.model.ParamVO;
import jwj8508.j2ee.basic.projectv5.model.BoardVO;
import mybatis3.helper.Pv10SqlSessionFactory;

import org.apache.ibatis.session.SqlSession;

public class BoardDAO {
	
	private static int perpage = 20;
	
	public static List<BoardVO> listBoard(int startno, String ftype, String fkey) {
		List<BoardVO> result = null;
		ParamVO pvo = new ParamVO();
		pvo.setStartno(startno);
		pvo.setPerpage(perpage);
		pvo.setFtype(ftype);
		pvo.setFkey(fkey);		
		SqlSession sess = Pv10SqlSessionFactory.openSession();
		try {
			result = sess.selectList("BoardMapper.findlistSQL", pvo);
		} finally {
			sess.close();
		}
		return result;
	}
	
	public static int totalBoard(String ftype, String fkey) {
		int result = -1;
		ParamVO pvo = new ParamVO();
		pvo.setFtype(ftype);
		pvo.setFkey(fkey);
		SqlSession sess = Pv10SqlSessionFactory.openSession();
		try {
			result = sess.selectOne("BoardMapper.totalSQL", pvo);
		} finally {
			sess.close();
		}		
		return result;		
	}
	
	public static BoardVO viewBoard(BoardVO b) {
		BoardVO result = new BoardVO();
		SqlSession sess = Pv10SqlSessionFactory.openSession();
		try {
			sess.selectOne("BoardMapper.viewcountSQL", b.getBdno());
			result = sess.selectOne("BoardMapper.viewSQL", b.getBdno());
		} finally {
			sess.close();
		}
		return result;
	}

	public static boolean newBoard(BoardVO b) {
		boolean result = false;
		int r = -1;
		SqlSession sess = Pv10SqlSessionFactory.openSession();
		try {
			r = sess.insert("BoardMapper.insertSQL", b);
			if(r>0) result = true;
		} finally {
			sess.close();
		}		
		return result;
	}

	public static boolean updateBoard(BoardVO b) {
		boolean result = false;
		int r = -1;
		SqlSession sess = Pv10SqlSessionFactory.openSession();
		try {
			r = sess.update("BoardMapper.updateSQL", b);
			if(r>0) result = true;
		} finally {
			sess.close();
		}
		return result;
	}
	
	public static boolean deleteBoard(BoardVO b) {
		boolean result = false;
		SqlSession sess = Pv10SqlSessionFactory.openSession();
		try {
			if(sess.delete("BoardMapper.deleteSQL", b.getBdno())>0)
				result = true;
		} finally {
			sess.close();
		}
		return result;
	}
	 
}
